package com.example.messagemodule.ui;

import androidx.annotation.RequiresApi;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.examination.mlib.constants.ARouterConstants;
import com.examination.mlib.constants.AllStringConstants;
import com.examination.mlib.constants.KitArouterConstants;
import com.example.messagemodule.ForbiddenAdapter;
import com.example.messagemodule.R;
import com.example.messagemodule.adapter.AttentionListAdapter;
import com.example.messagemodule.adapter.CarefulAdapter;
import com.example.messagemodule.adapter.CommonAdapter;
import com.example.messagemodule.adapter.DrugAdapter;
import com.example.messagemodule.adapter.LabelAdapter;
import com.example.messagemodule.adapter.ReactionsAdapter;
import com.example.messagemodule.adapter.StopDrugAdapter;
import com.example.messagemodule.adapter.UsageAdapter;
import com.example.messagemodule.entity.DirectionUsingMedicineEntity;
import com.example.messagemodule.entity.LabelEntity;
import com.example.messagemodule.utils.MessageUtils;
import com.google.gson.Gson;
import com.shicheng.mediaplayer.AudioSensorBinder;
import com.shicheng.mediaplayer.media.MediaCallBack;
import com.shicheng.mediaplayer.media.MediaManager;
import com.examination.mlib.baseold.ModuleBaseActivity;
import com.examination.mlib.interfaceapi.NetWorkCallBack;
import com.yilijk.base.network.RequestParams;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.network.https.interceptor.NetWorkUtils;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.CommonUtils;
import com.yilijk.base.utils.DevicesUtils;
import com.yilijk.base.utils.LoadingUtils;
import com.yilijk.base.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import crossoverone.statuslib.StatusUtil;

@Route(path = ARouterConstants.DirectionUsingMedicineActivity)
public class DirectionUsingMedicineActivity extends ModuleBaseActivity implements LifecycleOwner {
    //LifecycleRegistry 实现了Lifecycle
    private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(DirectionUsingMedicineActivity.this);
    AudioSensorBinder audioSensorBinder;

    @Autowired
    public String orderId;

    private Handler handler = new Handler();

    //    @SuppressLint("NonConstantResourceId")
    private TextView directionUsingMedicineActivityTitle;
    private ImageView directionUsingMedicineActivityBack;
    private TextView directionUsingMedicineActivityInfo;
    private TextView tipContentTv;
    private View tipContentView;
    private ImageView directionUsingStatus;
    private TextView directionUsingMedicineActivityStartTime;
    private SeekBar mediaSeekProgress;
    private TextView directionUsingMedicineActivityEndTime;
    private RecyclerView activityDirectionUsingMedicineRecyclerview1;
    private LinearLayout activityDrectionUsingMedicineLayout1;
    private RecyclerView activityDirectionUsingMedicineRecyclerview2;
    private LinearLayout activityDrectionUsingMedicineLayout2;
    private RecyclerView activityDirectionUsingMedicineRecyclerview3;
    private LinearLayout activityDrectionUsingMedicineLayout3;
    private ImageView activityDrectionUsingMedicineIcon4Close;
    private ImageView activityDrectionUsingMedicineIcon6Close;
    private ImageView activityDrectionUsingMedicineIcon7Close;
    private LinearLayout activityDirectionUsingMedicineCloseView4;
    private RecyclerView activityDirectionUsingMedicineRecyclerview4;
    private View activityDrectionUsingMedicineLayout3View;
    private LinearLayout activityDrectionUsingMedicineLayout4;
    private ImageView activityDrectionUsingMedicineIcon5Close;
    private LinearLayout activityDirectionUsingMedicineCloseView5;
    private LinearLayout activityDrectionUsingMedicineLayout4Middle1;
    private RecyclerView activityDirectionUsingMedicineRecyclerview5Middle1;
    private LinearLayout activityDrectionUsingMedicineLayout4Middle2;
    private RecyclerView activityDirectionUsingMedicineRecyclerview5Middle2;
    private View activityDrectionUsingMedicineLayout4View;
    private View activityDrectionUsingMedicineLayout5View;
    private View activityDrectionUsingMedicineLayout6View;
    private LinearLayout activityDrectionUsingMedicineLayout5;
    private RecyclerView activityDirectionUsingMedicineRecyclerview6;
    private LinearLayout activityDrectionUsingMedicineLayout6;
    private RecyclerView activityDirectionUsingMedicineRecyclerview7;
    private View activityDirectionUusingMedicineCloseView6;
    private View close6View;
    private View close7View;
    private View cartView7;
    private View close4View;
    private View close5View;

    //    private boolean isSeekBarChanging;//互斥变量，防止进度条与定时器冲突。
//    private MediaPlayer mediaPlayer = new MediaPlayer();
//    private Timer timer;
    SimpleDateFormat format;
    private boolean isPause = false;
    private boolean isCompletion = false;

    /**
     * data
     */
    private UsageAdapter usageAdapter;
    List<DirectionUsingMedicineEntity.DataBean.UsageBean> usage = new ArrayList<>();

    private AttentionListAdapter attentionListAdapter;
    List<String> attentionList = new ArrayList<>();

    private StopDrugAdapter stopDrugAdapter;
    List<String> noticeList = new ArrayList<>();

    private ReactionsAdapter reactionsAdapter;
    List<String> reactionsList = new ArrayList<>();

    private ForbiddenAdapter forbiddenAdapter;
    List<String> forbiddenList = new ArrayList<>();

    private CarefulAdapter carefulAdapter;
    List<String> carefulList = new ArrayList<>();

    private CommonAdapter commonAdapter;
    List<DirectionUsingMedicineEntity.DataBean.CommonListBean> commonList = new ArrayList<>();

    private DrugAdapter drugAdapter;
    List<DirectionUsingMedicineEntity.DataBean.DrugListBean> drugList = new ArrayList<>();

    private DirectionUsingMedicineEntity.DataBean directionUsingMedicineEntityData;


    PopupWindow popupWindow;
    View popupWindowView;
    TextView labelPopTitle;
    RecyclerView labelrecyclerView;
    private ImageView labelpopIcon;
    private String labelName;
    private ArrayList<LabelEntity.DataBean.LabelListBean> labelList = new ArrayList<>();
    private LabelAdapter labelAdapter;

    @Override
    public int bondLayout() {
        return R.layout.activity_direction_using_medicine;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void initView() {
        audioSensorBinder = new AudioSensorBinder(this);
        mLifecycleRegistry.addObserver(audioSensorBinder);

        directionUsingMedicineActivityTitle = (TextView) findViewById(R.id.directionUsingMedicineActivity_title);
        directionUsingMedicineActivityBack = (ImageView) findViewById(R.id.directionUsingMedicineActivity_back);
        directionUsingMedicineActivityInfo = (TextView) findViewById(R.id.directionUsingMedicineActivity_info);
        tipContentTv = (TextView) findViewById(R.id.txt_tip_content);
        tipContentView = findViewById(R.id.ll_tip_content);
        directionUsingStatus = (ImageView) findViewById(R.id.direction_using_status);
        directionUsingMedicineActivityStartTime = (TextView) findViewById(R.id.directionUsingMedicineActivity_startTime);
        mediaSeekProgress = (SeekBar) findViewById(R.id.media_seek_progress);
        directionUsingMedicineActivityEndTime = (TextView) findViewById(R.id.directionUsingMedicineActivity_endTime);
        activityDirectionUsingMedicineRecyclerview1 = (RecyclerView) findViewById(R.id.activity_direction_using_medicine_recyclerview1);
        activityDrectionUsingMedicineLayout1 = (LinearLayout) findViewById(R.id.activity_drection_using_medicine_layout1);
        activityDirectionUsingMedicineRecyclerview2 = (RecyclerView) findViewById(R.id.activity_direction_using_medicine_recyclerview2);
        activityDrectionUsingMedicineLayout2 = (LinearLayout) findViewById(R.id.activity_drection_using_medicine_layout2);
        activityDirectionUsingMedicineRecyclerview3 = (RecyclerView) findViewById(R.id.activity_direction_using_medicine_recyclerview3);
        activityDrectionUsingMedicineLayout3 = (LinearLayout) findViewById(R.id.activity_drection_using_medicine_layout3);
        activityDrectionUsingMedicineIcon4Close = (ImageView) findViewById(R.id.activity_drection_using_medicine_icon4_close);
        activityDrectionUsingMedicineIcon6Close = (ImageView) findViewById(R.id.activity_drection_using_medicine_icon6_close);
        activityDrectionUsingMedicineIcon7Close = (ImageView) findViewById(R.id.activity_drection_using_medicine_icon7_close);
        activityDirectionUsingMedicineCloseView4 = (LinearLayout) findViewById(R.id.activity_direction_using_medicine_closeView4);
        activityDirectionUsingMedicineRecyclerview4 = (RecyclerView) findViewById(R.id.activity_direction_using_medicine_recyclerview4);
        activityDrectionUsingMedicineLayout3View = (View) findViewById(R.id.activity_drection_using_medicine_layout3_view);
        activityDrectionUsingMedicineLayout4 = (LinearLayout) findViewById(R.id.activity_drection_using_medicine_layout4);
        activityDrectionUsingMedicineIcon5Close = (ImageView) findViewById(R.id.activity_drection_using_medicine_icon5_close);
        activityDirectionUsingMedicineCloseView5 = (LinearLayout) findViewById(R.id.activity_direction_using_medicine_closeView5);
        activityDrectionUsingMedicineLayout4Middle1 = (LinearLayout) findViewById(R.id.activity_drection_using_medicine_layout4_middle1);
        activityDirectionUsingMedicineRecyclerview5Middle1 = (RecyclerView) findViewById(R.id.activity_direction_using_medicine_recyclerview5_middle1);
        activityDrectionUsingMedicineLayout4Middle2 = (LinearLayout) findViewById(R.id.activity_drection_using_medicine_layout4_middle2);
        activityDirectionUsingMedicineRecyclerview5Middle2 = (RecyclerView) findViewById(R.id.activity_direction_using_medicine_recyclerview5_middle2);
        activityDrectionUsingMedicineLayout4View = (View) findViewById(R.id.activity_drection_using_medicine_layout4_view);
        activityDrectionUsingMedicineLayout5View = (View) findViewById(R.id.activity_drection_using_medicine_layout5_view);
        activityDrectionUsingMedicineLayout6View = (View) findViewById(R.id.activity_drection_using_medicine_layout6_view);
        activityDrectionUsingMedicineLayout5 = (LinearLayout) findViewById(R.id.activity_drection_using_medicine_layout5);
        activityDirectionUsingMedicineRecyclerview6 = (RecyclerView) findViewById(R.id.activity_direction_using_medicine_recyclerview6);
        activityDrectionUsingMedicineLayout6 = (LinearLayout) findViewById(R.id.activity_drection_using_medicine_layout6);
        activityDirectionUsingMedicineRecyclerview7 = (RecyclerView) findViewById(R.id.activity_direction_using_medicine_recyclerview7);
        activityDirectionUusingMedicineCloseView6 = findViewById(R.id.activity_direction_using_medicine_closeView6);
        close6View = findViewById(R.id.close_6);
        close7View = findViewById(R.id.close_7);
        cartView7 = findViewById(R.id.card_view_7);
        close4View = findViewById(R.id.close_4);
        close5View = findViewById(R.id.close_5);


//        ButterKnife.bind(this);
        StatusUtil.setUseStatusBarColor(this, Color.WHITE);
        StatusUtil.setSystemStatus(this, false, true);

        //view
        usageAdapter = new UsageAdapter(usage);
        activityDirectionUsingMedicineRecyclerview1.setLayoutManager(new LinearLayoutManager(this));
        activityDirectionUsingMedicineRecyclerview1.setAdapter(usageAdapter);
        usageAdapter.notifyDataSetChanged();


        FixGridLayout fixGridLayout_attention = new FixGridLayout();
        fixGridLayout_attention.setAutoMeasureEnabled(true);

        activityDirectionUsingMedicineRecyclerview2.setLayoutManager(fixGridLayout_attention);
        attentionListAdapter = new AttentionListAdapter(attentionList);
        activityDirectionUsingMedicineRecyclerview2.setAdapter(attentionListAdapter);
        attentionListAdapter.notifyDataSetChanged();

        FixGridLayout fixGridLayout_notice = new FixGridLayout();
        fixGridLayout_notice.setAutoMeasureEnabled(true);
        activityDirectionUsingMedicineRecyclerview3.setLayoutManager(fixGridLayout_notice);
        stopDrugAdapter = new StopDrugAdapter(noticeList);
        activityDirectionUsingMedicineRecyclerview3.setAdapter(stopDrugAdapter);
        stopDrugAdapter.notifyDataSetChanged();

        FixGridLayout fixGridLayout_reactions = new FixGridLayout();
        fixGridLayout_reactions.setAutoMeasureEnabled(true);
        activityDirectionUsingMedicineRecyclerview4.setLayoutManager(fixGridLayout_reactions);
        reactionsAdapter = new ReactionsAdapter(reactionsList);
        activityDirectionUsingMedicineRecyclerview4.setAdapter(reactionsAdapter);
        reactionsAdapter.notifyDataSetChanged();

        FixGridLayout fixGridLayout_forbidden = new FixGridLayout();
        fixGridLayout_forbidden.setAutoMeasureEnabled(true);
        activityDirectionUsingMedicineRecyclerview5Middle1.setLayoutManager(fixGridLayout_forbidden);
        forbiddenAdapter = new ForbiddenAdapter(forbiddenList);
        activityDirectionUsingMedicineRecyclerview5Middle1.setAdapter(forbiddenAdapter);
        forbiddenAdapter.notifyDataSetChanged();

        FixGridLayout fixGridLayout_careful = new FixGridLayout();
        fixGridLayout_careful.setAutoMeasureEnabled(true);
        activityDirectionUsingMedicineRecyclerview5Middle2.setLayoutManager(fixGridLayout_careful);
        carefulAdapter = new CarefulAdapter(carefulList);
        activityDirectionUsingMedicineRecyclerview5Middle2.setAdapter(carefulAdapter);
        carefulAdapter.notifyDataSetChanged();

        activityDirectionUsingMedicineRecyclerview6.setLayoutManager(new LinearLayoutManager(this));
        commonAdapter = new CommonAdapter(commonList);
        activityDirectionUsingMedicineRecyclerview6.setAdapter(commonAdapter);
        carefulAdapter.notifyDataSetChanged();

        activityDirectionUsingMedicineRecyclerview7.setLayoutManager(new LinearLayoutManager(this));
        drugAdapter = new DrugAdapter(drugList);
        activityDirectionUsingMedicineRecyclerview7.setAdapter(drugAdapter);
        drugAdapter.notifyDataSetChanged();

        directionUsingStatus.setImageDrawable(getResources().getDrawable(R.drawable.activity_voice_play));

        popupWindow = new PopupWindow();
        popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindowView = LayoutInflater.from(this).inflate(R.layout.popwindow_use_drug_guide, null);
        View popClose = popupWindowView.findViewById(R.id.labelpop_close);
        labelPopTitle = popupWindowView.findViewById(R.id.labelpop_title);
        labelpopIcon = (ImageView) popupWindowView.findViewById(R.id.labelpop_icon);

        labelrecyclerView = popupWindowView.findViewById(R.id.labelpop_list);
        labelrecyclerView.setLayoutManager(new LinearLayoutManager(this));
        labelAdapter = new LabelAdapter(labelList);
        labelrecyclerView.setAdapter(labelAdapter);
        labelAdapter.notifyDataSetChanged();
        popupWindow.setContentView(popupWindowView);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(false);
        ClickUtils.setFastOnClickListener(popClose, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                CommonUtils.setBackgroundAlpha(DirectionUsingMedicineActivity.this, 1.0f);
            }
        });
    }

    @Override
    public void initData() {
        format = new SimpleDateFormat("mm:ss");

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public void initEvent() {
        //用药注意事项
        ClickUtils.setAdapterItemFastOnClickListener(attentionListAdapter, new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                labelDetail(attentionList.get(position), "1");
            }
        });
        //停药须知
        ClickUtils.setAdapterItemFastOnClickListener(stopDrugAdapter, new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                labelDetail(noticeList.get(position), "2");
            }
        });
        //不良反应
        ClickUtils.setAdapterItemFastOnClickListener(reactionsAdapter, new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                labelDetail(reactionsList.get(position), "3");
            }
        });
        //禁用
        ClickUtils.setAdapterItemFastOnClickListener(forbiddenAdapter, new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                labelDetail(forbiddenList.get(position), "4");
            }
        });
        //慎用
        ClickUtils.setAdapterItemFastOnClickListener(carefulAdapter, new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                labelDetail(carefulList.get(position), "5");
            }
        });

        //用药小常识
        ClickUtils.setAdapterItemFastOnClickListener(commonAdapter, new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (commonList.get(position) != null && commonList.get(position).getCommonId() != 0) {
                    ARouter.getInstance().build(ARouterConstants.DirectionUsingMedicineDetailActivity)
                            .withLong("commonId", commonList.get(position).getCommonId())
                            .navigation();
                }
            }
        });

        //说明书
        ClickUtils.setAdapterItemFastOnClickListener(drugAdapter, new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                if (drugList.get(position) != null && drugList.get(position).getDrugId() != -1) {
                    bundle.putLong("drugId", drugList.get(position).getDrugId());
                }
                MessageUtils.push(DirectionUsingMedicineActivity.this,
                        KitArouterConstants.MY_BUNDLE,
                        KitArouterConstants.Mine.DrugGuide,
                        bundle);
            }
        });

        /**
         * 返回键
         */
        ClickUtils.setFastOnClickListener(directionUsingMedicineActivityBack, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        /**
         * 播放状态点击
         */
        ClickUtils.setFastOnClickListener(directionUsingStatus, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playVoice();
            }
        });

        ClickUtils.setFastOnClickListener(close4View, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickActivityDrectionUsingMedicineIcon4Close();
            }
        });

        ClickUtils.setFastOnClickListener(close6View, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickActivityDrectionUsingMedicineIcon6Close();
            }
        });


        ClickUtils.setFastOnClickListener(close7View, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickActivityDrectionUsingMedicineIcon7Close();
            }
        });


        ClickUtils.setFastOnClickListener(close5View, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickActivityDrectionUsingMedicineIcon5Close();
            }
        });
//        mediaSeekProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
////                isSeekBarChanging = true;
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//                isSeekBarChanging = false;
//                mediaPlayer.seekTo(seekBar.getProgress());
//            }
//        });
    }

    private void labelDetail(String lableName, String lableType) {
        LoadingUtils.getLoadingUtils(DirectionUsingMedicineActivity.this).showLoading();
        RequestParams jsonObject = new RequestParams();
        jsonObject.put("orderId", orderId);
        jsonObject.put("labelName", lableName);
        jsonObject.put("labelType", lableType);
        HttpUtils.post(AllStringConstants.MessageUrl.label, jsonObject, new HttpCallBack<LabelEntity>() {
            @Override
            public void onSuccess(LabelEntity labelEntity, int i) {
                if (labelEntity.isResult() && labelEntity.getData() != null) {
                    LabelEntity.DataBean data = labelEntity.getData();
                    if (data.getLabelName() != null && data.getLabelList() != null) {
                        showLabelPop(data.getLabelName(), data.getLabelList(), Integer.valueOf(lableType).intValue());
                    }
                }
                LoadingUtils.getLoadingUtils(DirectionUsingMedicineActivity.this).dismissLoading();
            }

            @Override
            public void onFailure(String s, int i) {
                LoadingUtils.getLoadingUtils(DirectionUsingMedicineActivity.this).dismissLoading();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MediaManager.removeAll();
        mLifecycleRegistry.removeObserver(audioSensorBinder);
    }

    void initMediaPlayer(boolean start) {
        MediaManager.builder()
                .setContext(this)
                .setOpenToast(true)
                .setProgressSeek(mediaSeekProgress)
                .setDebugModel(DevicesUtils.isEnvironment())
                .setUrl(directionUsingMedicineEntityData.getMp3Url())
                .setMediaListener(new MediaCallBack() {
                    @Override
                    public void progress(int i, int max) {
                        directionUsingMedicineActivityStartTime.setText(format.format(i) + "");
                    }

                    @Override
                    public void prepare(int duration, String time) {
                        directionUsingMedicineActivityEndTime.setText(format.format(duration) + "");
                        directionUsingMedicineActivityStartTime.setText("00:00");
                    }

                    @Override
                    public void error() {
                        ToastUtils.showShort("语音播放出错");
                    }

                    @Override
                    public void start() {

                    }

                    @Override
                    public void pause() {

                    }

                    @Override
                    public void stop() {

                    }

                    @Override
                    public void completion() {
                        isPause = false;
                        isCompletion = true;
                        directionUsingStatus.setImageDrawable(getResources().getDrawable(R.drawable.activity_voice_play));
//                        MediaManager.release();
                        mediaSeekProgress.setProgress(0);
                    }
                })
                .build();
        if (start) {
            MediaManager.start();
        }
    }


    void showLabelPop(String labelName, List<LabelEntity.DataBean.LabelListBean> name, int labelType) {

        handler.post(new Runnable() {
            @Override
            public void run() {
                if (labelType == 1) {
                    labelpopIcon.setImageDrawable(getDrawable(R.drawable.activity_drection_using_medicine_icon2));
                } else if (labelType == 2) {
                    labelpopIcon.setImageDrawable(getDrawable(R.drawable.activity_drection_using_medicine_icon3));
                } else if (labelType == 3) {
                    labelpopIcon.setImageDrawable(getDrawable(R.drawable.activity_drection_using_medicine_icon4));
                } else if (labelType == 4) {
                    labelpopIcon.setImageDrawable(getDrawable(R.drawable.activity_drection_using_medicine_icon5));
                } else if (labelType == 5) {
                    labelpopIcon.setImageDrawable(getDrawable(R.drawable.activity_drection_using_medicine_icon5));
                }
                labelList.clear();
                labelList.addAll(name);
                labelPopTitle.setText(labelName);
                labelAdapter.notifyDataSetChanged();
                if (popupWindow != null && !popupWindow.isShowing()) {
                    popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0);
                    CommonUtils.setBackgroundAlpha(DirectionUsingMedicineActivity.this, 0.3f);
                }
            }
        });
    }


    /**
     * 获取数据
     */

    void getData() {
        LoadingUtils.getLoadingUtils(this).showLoading();
        RequestParams jsonObject = new RequestParams();
        jsonObject.put("orderId", orderId);
        HttpUtils.post(AllStringConstants.MessageUrl.DirectionUsingMedicine, jsonObject, new HttpCallBack<DirectionUsingMedicineEntity>() {
            @Override
            public void onSuccess(DirectionUsingMedicineEntity directionUsingMedicineEntity, int i) {
                if (directionUsingMedicineEntity.isResult()) {
                    directionUsingMedicineEntityData = directionUsingMedicineEntity.getData();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            refreshUi();
                        }
                    });
                } else {
                    if (directionUsingMedicineEntity.getMsg() != null) {
                        Toast.makeText(DirectionUsingMedicineActivity.this, directionUsingMedicineEntity.getMsg() + "", Toast.LENGTH_SHORT).show();
                    }
                }
                LoadingUtils.getLoadingUtils(DirectionUsingMedicineActivity.this).dismissLoading();
            }

            @Override
            public void onFailure(String s, int i) {
                LoadingUtils.getLoadingUtils(DirectionUsingMedicineActivity.this).dismissLoading();
            }
        });
    }


    void refreshUi() {
        if (directionUsingMedicineEntityData == null) {
            Toast.makeText(this, "directionUsingMedicineEntityData数据不存在", Toast.LENGTH_SHORT).show();
            return;
        }

        if (directionUsingMedicineEntityData.getMp3Url() == null) {
        } else {
            if (directionUsingMedicineEntityData.getMp3Url().startsWith("http")) {
                initMediaPlayer(false);
            }
        }
        String userInfoString = "";
        if (directionUsingMedicineEntityData.getName() == null) {
            //TODO name is null
        } else {
            userInfoString += directionUsingMedicineEntityData.getName() + "  ";
        }
        if (directionUsingMedicineEntityData.getGenderName() == null) {
            //TODO GenderName is null
        } else {
            userInfoString += directionUsingMedicineEntityData.getGenderName() + "  ";
        }
//        if (directionUsingMedicineEntityData.getAge() == 0) {
//            //TODO Age is null
//        } else {
//            userInfoString += directionUsingMedicineEntityData.getAge()+"岁  ";
//        }

        if (TextUtils.isEmpty(directionUsingMedicineEntityData.getPatientAgeStr())) {
            //TODO Age is null
        } else {
            userInfoString += directionUsingMedicineEntityData.getPatientAgeStr();
        }
        directionUsingMedicineActivityInfo.setText(userInfoString);

        if (TextUtils.isEmpty(directionUsingMedicineEntityData.getTipContent())){
            tipContentView.setVisibility(View.GONE);
            tipContentTv.setText("");
        }else {
            tipContentView.setVisibility(View.VISIBLE);
            tipContentTv.setText(directionUsingMedicineEntityData.getTipContent());
        }
        if (directionUsingMedicineEntityData.getUsage() == null) {
            //TODO Usage is null
        } else {
            usage.clear();
            usage.addAll(directionUsingMedicineEntityData.getUsage());
            usageAdapter.notifyDataSetChanged();
        }

        if (directionUsingMedicineEntityData.getAttentionList() == null || directionUsingMedicineEntityData.getAttentionList().size() == 0) {
            activityDrectionUsingMedicineLayout1.setVisibility(View.GONE);
        } else {
            attentionList.clear();
            attentionList.addAll(directionUsingMedicineEntityData.getAttentionList());
            attentionListAdapter.notifyDataSetChanged();
        }

        if (directionUsingMedicineEntityData.getNoticeList() == null || directionUsingMedicineEntityData.getNoticeList().size() == 0) {
            activityDrectionUsingMedicineLayout2.setVisibility(View.GONE);
        } else {
            noticeList.clear();
            noticeList.addAll(directionUsingMedicineEntityData.getNoticeList());
            stopDrugAdapter.notifyDataSetChanged();
        }

        if (directionUsingMedicineEntityData.getReactionsList() == null || directionUsingMedicineEntityData.getReactionsList().size() == 0) {
            activityDrectionUsingMedicineLayout3.setVisibility(View.GONE);
            activityDrectionUsingMedicineLayout3View.setVisibility(View.GONE);
        } else {
            reactionsList.clear();
            reactionsList.addAll(directionUsingMedicineEntityData.getReactionsList());
            reactionsAdapter.notifyDataSetChanged();
        }

        if ((directionUsingMedicineEntityData.getCarefulList() == null && directionUsingMedicineEntityData.getForbiddenList() == null) ||
                (directionUsingMedicineEntityData.getCarefulList() != null && directionUsingMedicineEntityData.getForbiddenList() != null &&
                        directionUsingMedicineEntityData.getCarefulList().size() == 0 && directionUsingMedicineEntityData.getForbiddenList().size() == 0
                )) {
            activityDrectionUsingMedicineLayout4.setVisibility(View.GONE);
            activityDrectionUsingMedicineLayout4View.setVisibility(View.GONE);
        } else {
            if (directionUsingMedicineEntityData.getForbiddenList() == null || directionUsingMedicineEntityData.getForbiddenList().size() == 0) {
                activityDrectionUsingMedicineLayout4Middle1.setVisibility(View.GONE);
            } else {
                forbiddenList.clear();
                forbiddenList.addAll(directionUsingMedicineEntityData.getForbiddenList());
                forbiddenAdapter.notifyDataSetChanged();
            }
            if (directionUsingMedicineEntityData.getCarefulList() == null || directionUsingMedicineEntityData.getCarefulList().size() == 0) {
                activityDrectionUsingMedicineLayout4Middle2.setVisibility(View.GONE);
            } else {
                carefulList.clear();
                carefulList.addAll(directionUsingMedicineEntityData.getCarefulList());
                carefulAdapter.notifyDataSetChanged();
            }
        }

        if (directionUsingMedicineEntityData.getCommonList() == null) {
            activityDrectionUsingMedicineLayout5.setVisibility(View.GONE);
            activityDrectionUsingMedicineLayout5View.setVisibility(View.GONE);
        } else {
            commonList.clear();
            commonList.addAll(directionUsingMedicineEntityData.getCommonList());
            commonAdapter.notifyDataSetChanged();
        }

        if (directionUsingMedicineEntityData.getDrugList() == null) {
            activityDrectionUsingMedicineLayout6.setVisibility(View.GONE);
            activityDrectionUsingMedicineLayout6View.setVisibility(View.GONE);
        } else {
            drugList.clear();
            drugList.addAll(directionUsingMedicineEntityData.getDrugList());
            drugAdapter.notifyDataSetChanged();
        }
    }


    void playVoice() {
        if (directionUsingMedicineEntityData != null && directionUsingMedicineEntityData.getMp3Url() != null && directionUsingMedicineEntityData.getMp3Url().startsWith("http")) {
            if (isPause) {
                isPause = false;
                MediaManager.resume();//开始播放
                directionUsingStatus.setImageDrawable(getResources().getDrawable(R.drawable.activity_voice_playing));
            } else {
                if (!MediaManager.isPlaying()) {
                    if (isCompletion) {
                        isCompletion = false;
                        MediaManager.start();
//                        initMediaPlayer(true);
                    } else {
                        MediaManager.start();
                    }
                    directionUsingStatus.setImageDrawable(getResources().getDrawable(R.drawable.activity_voice_playing));
                } else {
                    isPause = true;
                    MediaManager.pause();
                    directionUsingStatus.setImageDrawable(getResources().getDrawable(R.drawable.activity_voice_play));
                }
            }
        }
    }

    void ClickActivityDrectionUsingMedicineIcon4Close() {
        int visibility = activityDirectionUsingMedicineCloseView4.getVisibility();
        if (visibility == View.GONE) {
            activityDirectionUsingMedicineCloseView4.setVisibility(View.VISIBLE);
            activityDrectionUsingMedicineIcon4Close.setImageDrawable(getResources().getDrawable(R.drawable.icon_up));
        } else {
            activityDrectionUsingMedicineIcon4Close.setImageDrawable(getResources().getDrawable(R.drawable.icon_down));
            activityDirectionUsingMedicineCloseView4.setVisibility(View.GONE);
        }
    }

    void ClickActivityDrectionUsingMedicineIcon6Close() {
        int visibility = activityDirectionUusingMedicineCloseView6.getVisibility();
        if (visibility == View.GONE) {
            activityDirectionUusingMedicineCloseView6.setVisibility(View.VISIBLE);
            activityDrectionUsingMedicineIcon6Close.setImageDrawable(getResources().getDrawable(R.drawable.icon_up));
        } else {
            activityDrectionUsingMedicineIcon6Close.setImageDrawable(getResources().getDrawable(R.drawable.icon_down));
            activityDirectionUusingMedicineCloseView6.setVisibility(View.GONE);
        }
    }

    void ClickActivityDrectionUsingMedicineIcon7Close() {
        int visibility = cartView7.getVisibility();
        if (visibility == View.GONE) {
            cartView7.setVisibility(View.VISIBLE);
            activityDrectionUsingMedicineIcon7Close.setImageDrawable(getResources().getDrawable(R.drawable.icon_up));
        } else {
            activityDrectionUsingMedicineIcon7Close.setImageDrawable(getResources().getDrawable(R.drawable.icon_down));
            cartView7.setVisibility(View.GONE);
        }
    }


    void ClickActivityDrectionUsingMedicineIcon5Close() {
        int visibility = activityDirectionUsingMedicineCloseView5.getVisibility();
        if (visibility == View.GONE) {
            activityDirectionUsingMedicineCloseView5.setVisibility(View.VISIBLE);
            activityDrectionUsingMedicineIcon5Close.setImageDrawable(getResources().getDrawable(R.drawable.icon_up));
        } else {
            activityDrectionUsingMedicineIcon5Close.setImageDrawable(getResources().getDrawable(R.drawable.icon_down));
            activityDirectionUsingMedicineCloseView5.setVisibility(View.GONE);
        }
    }

    ;
}